package net.qiqbframework.loadhanding;

import lombok.Setter;
import net.qiqbframework.messaging.GenericMessage;
import net.qiqbframework.messaging.MessageDecorator;
import net.qiqbframework.modelling.domain.AggregateModel;
import net.qiqbframework.modelling.domain.BizIdentifierModel;

import javax.annotation.Nonnull;
import java.util.HashMap;


public class GenericLoadMessage<ID, A> extends MessageDecorator<ID> implements LoadMessage<ID, A> {

    private final String businessIdName;


    private final AggregateModel aggregateModel;


    private LoadMessage<ID, A> nextLoad;

    @Setter
    private boolean enableCaching = false;

    public GenericLoadMessage(@Nonnull ID businessId, String businessIdName, @Nonnull AggregateModel<A> aggregateModel) {
        super(new GenericMessage<>(businessId, new HashMap<>()));
        this.aggregateModel = aggregateModel;
        this.businessIdName = businessIdName;
    }

    /**
     * 没有业务ID的加载消息
     *
     * @param
     */
    public GenericLoadMessage(@Nonnull AggregateModel<A> aggregateModel) {
        super(new GenericMessage<>(null, new HashMap<>()));
        this.aggregateModel = aggregateModel;
        // 空字符串作为没有业务ID的加载器
        this.businessIdName = "_empty";
    }


    @Override
    protected String describeType() {
        return "GenericLoadMessage";
    }


    @Override
    public String getAggregateNaming() {
        return aggregateModel.modelName();
    }

    @Override
    public String getBusinessIdName() {
        return businessIdName;
    }

    @Override
    public Class<A> getAggregateType() {
        return aggregateModel.entityClass();
    }

    @Override
    public AggregateModel<A> getAggregateModel() {
        return aggregateModel;
    }

    @Override
    public BizIdentifierModel<ID> getBizIdentifierModel() {
        return aggregateModel.getBizIdentifierModel(businessIdName);
    }

    @Override
    public boolean enableCaching() {
        return enableCaching;
    }

    @Override
    public LoadMessage<ID, A> next() {
        return nextLoad;
    }

    @Override
    public void next(LoadMessage<ID, A> nextLoad) {
        this.nextLoad = nextLoad;
    }
}
